<template>
  <view class="login-container">
    <!-- 顶部背景 -->
    <view class="top-bg"></view>
    
    <!-- 登录卡片 -->
    <view class="login-card">
      <!-- 标题 -->
      <view class="login-title">欢迎登录</view>
      
      <!-- 表单区域 -->
      <view class="form-area">
        <!-- 用户名 -->
        <view class="input-group">
          <uni-icons type="person" size="20" color="#999" class="input-icon"></uni-icons>
          <input 
            type="text" 
            v-model="username" 
            placeholder="请输入用户名/手机号" 
            class="input-field"
            placeholder-class="placeholder"
          />
        </view>
        
        <!-- 密码 -->
        <view class="input-group">
          <uni-icons type="locked" size="20" color="#999" class="input-icon"></uni-icons>
          <input 
            :type="showPassword ? 'text' : 'password'" 
            v-model="password" 
            placeholder="请输入密码" 
            class="input-field"
            placeholder-class="placeholder"
          />
          <uni-icons 
            :type="showPassword ? 'eye' : 'eye-slash'" 
            size="20" 
            color="#999" 
            class="password-toggle"
            @click="showPassword = !showPassword"
          ></uni-icons>
        </view>
        
        <!-- 记住密码和忘记密码 -->
        <view class="form-options">
          <view class="remember-me">
            <checkbox-group @change="toggleRemember">
              <label class="checkbox-label">
                <checkbox :checked="rememberMe" color="#07c160" style="transform:scale(0.8)"/>
                <text>记住密码</text>
              </label>
            </checkbox-group>
          </view>
          <view class="forgot-password" @click="forgotPassword">忘记密码?</view>
        </view>
        
        <!-- 登录按钮 -->
        <button class="login-btn" @click="handleLogin">登录</button>
      </view>
      
      <!-- 第三方登录 -->
      <view class="third-party-login">
        <view class="divider">
          <view class="divider-line"></view>
          <view class="divider-text">其他方式登录</view>
          <view class="divider-line"></view>
        </view>
        
        <!-- 微信登录 -->
        <view class="wechat-login" @click="wechatLogin">
          <view class="wechat-icon">
            <image src="/static/weixin-2.png" class="weixin-2" />
          </view>
          <text>微信一键登录</text>
        </view>
      </view>
      
      <!-- 注册链接 -->
      <view class="register-link">
        还没有账号? <text class="register-text" @click="goToRegister">立即注册</text>
      </view>
    </view>
    
    <!-- 底部装饰 -->
    <view class="bottom-decor">
      <view class="circle circle1"></view>
      <view class="circle circle2"></view>
      <view class="circle circle3"></view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { usePublicStore } from '../../store/public';

// 响应式数据
const app =getApp();
const username = ref('');
const password = ref('');
const rememberMe = ref(true);
const showPassword = ref(false);
const userInfo = usePublicStore();

// 切换密码可见性
const toggleRemember = () => {
  rememberMe.value = !rememberMe.value;
};

// 处理登录
const handleLogin = () => {
  if (!username.value || !password.value) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    });
    return;
  }
  
  uni.showLoading({
    title: '登录中...',
    mask: true
  });
  
  // 模拟登录请求
  setTimeout(() => {
    const baseUrl = app.globalData.baseUrl;
    uni.request({
    	url: encodeURI(baseUrl + '/login/login'),
    	method: 'GET',
    	data: {
    		"username":username.value,
    		"password":password.value
    	},
    	header: {
    		"X-Tenant-Id": "all"
    	},
    	success: (res) => {
    		console.log(res.data!.data)
			const data = res.data!.data
			const tokenData = data!.tokenValue!.tokenValue
			const userData = data!.userValue
			
			uni.setStorageSync("token",tokenData);
			uni.setStorageSync("userId",userData.id);
			uni.setStorageSync("tenantId",userData.tenantId)
			uni.setStorageSync("type",userData.type)
			userInfo.updateUserInfo();
    	}
    });
    uni.showToast({
      title: '登录成功',
      icon: 'success'
    });
    
    // 登录成功后跳转到首页
    setTimeout(() => {
      uni.navigateTo({
        url: '/pages/index/index'
      });
    }, 1500);
  }, 2000);
};

// 微信登录
const wechatLogin = () => {
  uni.showLoading({
    title: '微信登录中...',
    mask: true
  });
  
  // 模拟微信登录过程
  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({
      title: '微信登录成功',
      icon: 'success'
    });
    
    // 登录成功后跳转到首页
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/index/index'
      });
    }, 1500);
  }, 2000);
};

// 忘记密码
const forgotPassword = () => {
  uni.showToast({
    title: '请通过手机号找回密码',
    icon: 'none'
  });
};

// 跳转到注册
const goToRegister = () => {
  uni.navigateTo({
    url: '/pages/login/regiest/regiest'
  });
};

// 页面加载时检查本地存储的登录信息
onLoad(() => {
  const savedUsername = uni.getStorageSync('username');
  const savedPassword = uni.getStorageSync('password');
  
  if (savedUsername && savedPassword) {
    username.value = savedUsername;
    password.value = savedPassword;
  }
});
</script>

<style lang="scss">
page {
  height: 100%;
  background-color: #f5f7fa;
}

.login-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.top-bg {
  height: 250rpx;
  background: linear-gradient(135deg, #1e9bff, #0d7ddc);
  border-bottom-left-radius: 40rpx;
  border-bottom-right-radius: 40rpx;
}

.login-card {
  background-color: #ffffff;
  border-radius: 24rpx;
  box-shadow: 0 10rpx 40rpx rgba(0, 0, 0, 0.08);
  margin: -120rpx 40rpx 0;
  padding: 60rpx 40rpx;
  position: relative;
  z-index: 2;
}

.login-title {
  font-size: 44rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 60rpx;
}

.form-area {
  margin-bottom: 50rpx;
}

.input-group {
  position: relative;
  margin-bottom: 40rpx;
  border-bottom: 1rpx solid #e6e6e6;
  padding-bottom: 20rpx;
  display: flex;
  align-items: center;
}

.input-icon {
  margin-right: 20rpx;
}

.input-field {
  flex: 1;
  height: 60rpx;
  font-size: 30rpx;
  color: #333;
}

.password-toggle {
  position: absolute;
  right: 0;
}

.placeholder {
  color: #999;
  font-size: 28rpx;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 50rpx;
  font-size: 26rpx;
  color: #666;
}

.checkbox-label {
  display: flex;
  align-items: center;
}

.forgot-password {
  color: #1e9bff;
}

.login-btn {
  background: linear-gradient(to right, #1e9bff, #0d7ddc);
  color: #fff;
  border-radius: 50rpx;
  height: 88rpx;
  line-height: 88rpx;
  font-size: 34rpx;
  box-shadow: 0 8rpx 20rpx rgba(13, 125, 220, 0.3);
}

.third-party-login {
  margin-top: 60rpx;
}

.divider {
  display: flex;
  align-items: center;
  margin-bottom: 50rpx;
}

.divider-line {
  flex: 1;
  height: 1rpx;
  background-color: #e6e6e6;
}

.divider-text {
  padding: 0 20rpx;
  color: #999;
  font-size: 26rpx;
}

.wechat-login {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #666;
  font-size: 26rpx;
}

.wechat-icon {
  width: 100rpx;
  height: 100rpx;
  background-color: #07c160;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20rpx;
  box-shadow: 0 8rpx 20rpx rgba(7, 193, 96, 0.3);
}

.weixin-2 {
	width: 68rpx;
	height: 68rpx;
}

.register-link {
  text-align: center;
  margin-top: 50rpx;
  color: #666;
  font-size: 28rpx;
}

.register-text {
  color: #1e9bff;
  font-weight: bold;
}

.bottom-decor {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 150rpx;
  display: flex;
  justify-content: space-around;
}

.circle {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background-color: rgba(13, 125, 220, 0.1);
}

.circle1 {
  transform: translateY(60rpx) translateX(-30rpx);
}

.circle2 {
  transform: translateY(30rpx);
}

.circle3 {
  transform: translateY(80rpx) translateX(30rpx);
}
</style>
